<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\app\controller\v1;
use app\app\Appbase;
use base\files\Poster;
use app\admin\model\SystemParamValue;
use app\common\service\Coupon as CouponService;
use think\facade\Session;

class Coupon extends Appbase 
{
	
	/**
	* 可取优惠券
	* /app/v1/coupon/canReceive
	* 需要登录 
	* @is_need_login false
	*/
	public function canReceive($goods_id){
		try {
			if(request()->userId){
				$result = model("Coupon")->userCanReceive(request()->userId,$goods_id);
			}else{
				$result = [];
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 领取优惠券
	* /app/v1/coupon/receive
	* 需要登录 
	* @is_need_login true
	*/
	public function receive($coupon_id){
		try {
			$result = model("CouponUser")->receive($coupon_id,request()->userId);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取用户优惠券列表
	* /app/v1/coupon/getUserCouponList
	* 需要登录 
	* @is_need_login true
	*/
	public function getUserCouponList($state='',$is_give=0){
		try {
			$result = model('CouponUser')->getUserCouponList(request()->userId,$state,$is_give);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取优惠券明细
	* /app/v1/coupon/details
	* 需要登录 
	* @is_need_login true
	*/
	public function details($id=''){
		try {
			$result = model('CouponUser')->details($id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 生成优惠券海报
	* /admin/v1/coupon/generatePoster
	* 需要登录 
	* @is_need_login false
	*/
	public function generatePoster($cid,$bg='',$size=0,$x=0,$y=0){
		try {
			$uid = request()->userId;
			
			//获取优惠券数据
			$coupon = model('CouponUser')->where('id',$cid)->find();
			$couponId = model('CouponIssue')->where('id',$coupon['coupon_id'])->value('cid');
			$data = model('coupon')->where('id',$couponId)->find();
			if(!$data){
				return $this->ReError("优惠券不存在！");
			}
			
			//参数二维码路由
			$url = SystemParamValue::where('param_code','coupon_qrcode_url')->value("param_value");
			$url = $url.'?uid='.$uid.'&code='.$coupon['coupon_code'].'&cid='.$coupon['id'];
			$url = $coupon['coupon_code'];
			
			$bg = $bg ?: $data['bg_path'];
			$size = $size ?: $data['code_size'];
			$x = $x?:$data['code_x'];
			$y = $y?:$data['code_y'];
			
			Poster::generate($bg,$url,$size,$x,$y);

		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}

	
	/**
	* 获取优惠券二维码
	* /admin/v1/coupon/qrCode
	* 需要登录 
	* @is_need_login false
	*/
	public function qrCode($id,$bg='',$size=0,$x=0,$y=0){
		try {
			$uid = request()->userId;
			
			//获取优惠券数据
			if( !$coupon = model('CouponUser')->where('uid',$uid)->where('id',$id)->find() ){
				return $this->ReError("优惠券不存在！");
			}
			
			$result = Poster::qrCode($id,150,'coupon');
			
			return $this->ReSucess($result,'5分钟内过期，请尽快使用！');	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取优惠券核销列表
	* /app/v1/coupon/getWriteOffList
	* 需要登录 
	* @is_need_login true
	*/
	public function getWriteOffList(){
		$uid = request()->userId;
		try {
			$identitys = explode(',',request()->userInfo['identity']);
			if( in_array('2', $identitys) ){
				$result = model('CouponWriteOff')->getShopWriteOffList($uid);
			}else{
				$result = model('CouponWriteOff')->getClerkWriteOffList($uid);
			}
			return $this->ReSucess($result);	
			
		} catch (Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	
	/**
	* 核销优惠券
	* /app/v1/coupon/writeOff
	* 需要登录 
	* @is_need_login true
	*/
	public function writeOff($code){
		$service = new CouponService;
		$result = $service->writeOff($code);
		if(!$result){
			return $this->ReSucess(0,$service->errorInfo);	
		}else{
			return $this->ReSucess(1,"核销成功！");	
		}
	}

	
}

